/*
 *  Author: zhaijl 
 *  Url: http://218.1.73.212/down.php
 *  Country: China
 *  
 * ====================================================================
 *
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * ====================================================================
 *
 * This software based apache mina 2.0 and spring 3.0
 *
 */
package org.cloudy.http.codec;

import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.util.Iterator;
import java.util.Map.Entry;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

public class Encoder implements ProtocolEncoder {
	private static final byte[] CRLF = new byte[] { 0x0D, 0x0A } ;
	 
	public Encoder () {}
	
    public void encode ( IoSession iosession, Object object, ProtocolEncoderOutput protocolencoderoutput )
		throws Exception {
    	Response response = ( Response ) object ;
        IoBuffer buffer = IoBuffer.allocate ( 256 ) ;
        buffer.setAutoExpand(true);

        CharsetEncoder encoder = Charset.defaultCharset ().newEncoder () ;
        if ( response.getResponseCode () != Response.FILE_STATUS_SUCCESS ) {
        	buffer.putString ( "HTTP/1.1 ", encoder ) ;
        	buffer.putString ( String.valueOf ( response.getResponseCode () ), encoder ) ;
        
            switch ( response.getResponseCode () ) {
            case Response.HTTP_STATUS_SUCCESS :
            	buffer.putString ( " OK", encoder ) ;
                break ;
            case Response.HTTP_STATUS_NOT_FOUND :
            	buffer.putString ( " Not Found", encoder ) ;
                break;
            }
            
            buffer.put ( CRLF ) ;
            
            for ( Iterator< Entry< String, String > > it = response.getHeaders ().entrySet ().iterator () ; it.hasNext () ; ) {
                Entry< String, String > entry = ( Entry< String, String > ) it.next () ;
                buffer.putString ( (String) entry.getKey (), encoder ) ;
                buffer.putString ( ": ", encoder);
                buffer.putString ( (String) entry.getValue (), encoder ) ;
                buffer.put ( CRLF ) ;
            }

            buffer.put ( CRLF ) ;
        }            
        // add body
        buffer.put ( response.getBody ().array (), 0, response.getBodyLength() ) ;
        buffer.flip () ;
       
        protocolencoderoutput.write ( buffer ) ;   
    }
    
    public void dispose ( IoSession iosession ) throws Exception {}
}